cap prog drop cexceldlg
prog define  cexceldlg
	
	syntax, [file(string)     ///
			 dirc(string)  exceldir(string)]


if (`"`file'"'!=""&`"`dirc'"'!=""){

	di _n "note: dirc() is ignored as file() is specified"

}
	

if (`"`file'"'==""){
	if(`"`dirc'"'==""){
		di as error "either file() or dirc() should be specified"
		error 198
	}
	else{

		local file `dirc'
	}
}


	// get the install file names of anything
	di _n(1) "{title:Executing the dxls command}" _n
	di as txt "dxls `file'"   _n 
	
	// call the dxls function 
	*dxls2 `file',exceldir(`"`exceldir'"')   
	dxls `"`file'"',exceldir(`"`exceldir'"')     
	
end


/*
cap program drop dxls2
program define dxls2

 version 14
 
 syntax [anything], [EXCeldir(string)]
	local anything `anything'
	if `"`anything'"'==""{
		db cexcel
		exit
	}
	if `"`exceldir'"'!=""{
		mata: st_local("path", pathgetparent(`"`exceldir'"')) 
		cap findfile EXCEL.EXE, path(`"`path'"')
		if _rc{
			di as red `"file "EXCEL.EXE" not found in exceldir()"'
			exit				
		}
	
	}
	else{
		local dirlist: dir "C:\Program Files\Microsoft Office\root\" dirs "office*"
		foreach fdir in `dirlist'{
			cap findfile EXCEL.EXE, path("C:\Program Files\Microsoft Office\root\`fdir'")
			if _rc==0{
				local exceldir "C:\Program Files\Microsoft Office\root\`fdir'"
				continue, break
			}
		}
		if _rc!==0{
			di as red `"Error: EXCEL.EXE not found"'
			di as red `"Pls Specifiy EXCEL.EXE path in exceldir()"'
			exit
		}
		
	}
	tempname flag
	mata: analyzea(`"`anything'"',"`flag'")
	
	if `flag'==-1{
		di as red `"Error: file [`anything'] not found"'
		exit
	}
	if `flag'==-2{
		di as red `"Error: Directory [`anything'] not exist"'
		exit
	}
	local xlmpath=c(sysdir_plus)
	if `flag'==1{
		cap findfile dxls.xlsm, path(`"`xlmpath'd/"')
		if _rc {
			di as red `"Error: file "dxls.xlsm" not placed in [`xlmpath'd/] "'
			exit			
		}
		mata:writebat("_tmp`flag'.bat",`"`xlmpath'd/dxls.xlsm"',`"`anything'"',`"`exceldir'"')
	}
	else{
		cap findfile dxlsall.xlsm, path(`"`xlmpath'd/"')
		if _rc {
			di as red `"Error: file "dxlsall.xlsm" not placed in [`xlmpath'd/]"'
			exit			
		}		
		mata:writebat("_tmp`flag'.bat",`"`xlmpath'd/dxlsall.xlsm"',`"`anything'"',`"`exceldir'"')
	}
	
	
	local pwd=c(pwd)
	!"`pwd'\_tmp`flag'.bat"
	cap erase _tmp`flag'.bat
 
 end
 
 
 
 cap mata mata drop analyzea()
 cap mata mata drop writebat()
 
mata:

void function analyzea(string scalar path,string scalar flag)
{
	path2= pathbasename(path)
	if( usubstr(path2,-4,4)==".xls"|usubstr(path2,-5,5)==".xlsx"){
		xflag=1
		if(!fileexists(path)){
			xflag=-1
			 if( direxists(path)) {
				xflag=2
			 }				
		}
	}
	else{
		 if( direxists(path)) {
		 	xflag=2
		 }
		 else{
		 	xflag=-2
		 }
	}
	
	st_numscalar(flag,xflag)
	
	
}


void function writebat(string scalar pathbat,string scalar xlmpath,
                       string scalar xlspath,string scalar exceldir)
{
	writefile=fopen(pathbat,"rw")
	abc=`"'dir /a/b/s/on "%ProgramFiles%\*%exeName%""'
    cdc="'"
	vcv="%exePath%"
	fwrite(writefile, sprintf(`"set exeName=EXCEL.exe  \n"'))
	if(exceldir==""){
		fwrite(writefile, sprintf(`"for /f "tokens=*" %s in (%s%s) do (set exePath="%s") \n"',"%%i",abc,cdc,"%%i"))
		fwrite(writefile, sprintf("Set MacroName=clearpassword  \n"))
		fwrite(writefile, sprintf(`"Set MacroName2=%s  \n"',xlspath))	
		fwrite(writefile, sprintf(`"%s "%s" \n"',vcv,xlmpath))	
	}
	else{
		fwrite(writefile, sprintf("Set MacroName=clearpassword  \n"))
		fwrite(writefile, sprintf(`"Set MacroName2=%s\n"',xlspath))	
		fwrite(writefile, sprintf(`""%s" "%s" \n"',exceldir,xlmpath))			
	}

	fwrite(writefile, sprintf("exit\n"))	
	fclose(writefile)

}



end


*/

